<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>InitPHP框架 PHP框架 - A PHP Framework - Controller常用API</title>
<link href="static/common.css" type="text/css" rel="stylesheet" />
<meta name="keywords" content="php框架，国产php框架, initphp框架，MVC，分层体系" />
<meta name="description" content="initphp框架是一款国产php框架。initphp框架主要基于MVC模式，具备代码清晰，操作简单，功能齐全，开发快速，高效安全等特点，是您选择php框架的首选。" />
</head>
<body>
<div class="header">
  <div class="header_nav">InitPHP (A PHP Framework) 用户手册</div>
  <div class="header_div">
    <div class="header_title">第6章 6.2.1 Controller常用API</div>
    <div class="header_right"><a href="6_1.htm">上一页</a> <a href="6_2_2.htm">下一页</a> </div>
  </div>
</div>
<div class="title">目录</div>
<ul style="list-style:none;">
  <li><a href="#$this->controller->ajax_return">$this->controller->ajax_return</a></li>
  <li><a href="#$this->controller->check_token">$this->controller->check_token</a></li>
  <li><a href="#$this->controller->get_token">$this->controller->get_token</a></li>
  <li><a href="#$this->controller->redirect">$this->controller->redirect</a></li>
  <li><a href="#$this->controller->return404">$this->controller->return404</a></li>
  <li><a href="#$this->controller->return200">$this->controller->return200</a></li>
  <li><a href="#$this->controller->return500">$this->controller->return500</a></li>
  <li><a href="#$this->controller->return403">$this->controller->return403</a></li>
  <li><a href="#$this->controller->return405">$this->controller->return405</a></li>
  <li><a href="#$this->controller->check_service_return">$this->controller->check_service_return</a></li>
</ul>
<div class="nav"></div>
<div class="title">1. $this->controller->ajax_return($status, $message = '', $data = array(), $type = 'json') AJAX返回数据结构<a name="$this->controller->ajax_return"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>一般在AJAX提交后，通过这个函数返回Controller执行的数据结构。在Controller中使用。
如果是jsonp，则URL中需要带上callback参数</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
  <tr>
    <td>$status</td>
    <td>Int </td>
    <td>是</td>
    <td>0:错误信息|1:正确信息</td>
  </tr>
  <tr>
    <td>$message</td>
    <td>String </td>
    <td>否</td>
    <td>显示的信息</td>
  </tr>
  <tr>
    <td>$data</td>
    <td>Array </td>
    <td>否</td>
    <td>订单数据</td>
  </tr>
  <tr>
    <td>$type</td>
    <td>String</td>
    <td>否</td>
    <td>返回数据类型，json|xml|eval|jsonp</td>
  </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		$this->controller->ajax_return(1, '操作成功'); //直接返回JSON数据 
	}
	
	public function test() {
		echo 'Hello World';
	}
	
	/**
	 * @return testService
	 */
	private function getTestService() {
		return InitPHP::getService('test','test');
	}

}
</pre>
<div class="nav"></div>
<div class="title">2. $this->controller->check_token() 检测TOKEN<a name="$this->controller->check_token"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>可以检测提交过来的TOKEN是否正确，正确返回TRUE，错误返回FALSE。可以设置是否必须POST方式提交，InitPHP建议采用POST方式提交。在Controller中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		if ($this->controller->check_token() == false) {
			exit('Token 不正确，不能提交数据');
		} 
		$this->view->display(); //模板显示
	}
	
	public function test() {
		echo 'Hello World';
	}
	
	/**
	 * @return testService
	 */
	private function getTestService() {
		return InitPHP::getService('test','test');
	}

}
</pre>
<div class="nav"></div>
<div class="title">3. $this->controller->get_token() 获取TOKEN<a name="$this->controller->get_token"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>TOKEN，每个用户登录后都会生成一个随即TOKEN，只有拿着这个TOKEN才可以在网站上进行正常访问。在GET和POST提交数据的地方都需要带上TOKEN，可以防止CSRF攻击，提高网站安全性。TOKEN会自动生成。在Controller中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		echo $this->controller->get_token(); //获取TOKEN
	}
	
	public function test() {
		echo 'Hello World';
	}
	
	/**
	 * @return testService
	 */
	private function getTestService() {
		return InitPHP::getService('test','test');
	}

}
</pre>
<div class="nav"></div>
<div class="title">4. $this->controller->redirect($url, $time = 0) 重定向<a name="$this->controller->redirect"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>重定向函数，可以自定义重定向URL和跳转的时间。在Controller中使用</p>
<div class="title">
  <h3>参数：</h3>
</div>
<table>
  <tr>
    <th style="width:20%;"> 参数 </th>
    <th style="width:20%;"> 类型 </th>
    <th style="width:10%;"> 是否必须 </th>
    <th style="width:50%;"> 描述 </th>
  </tr>
  <tr>
    <td>$url</td>
    <td>String </td>
    <td>是</td>
    <td>URL</td>
  </tr>
  <tr>
    <td>$time</td>
    <td>Int</td>
    <td>否</td>
    <td>重定向时间</td>
  </tr>
</table>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		$this->controller->redirect('http://www.baidu.com', 3); //3秒后跳转到百度
	}
	
	public function test() {
		echo 'Hello World';
	}
	
	/**
	 * @return testService
	 */
	private function getTestService() {
		return InitPHP::getService('test','test');
	}

}
</pre>

<div class="nav"></div>
<div class="title">5. $this->controller->return404() 返回http404<a name="$this->controller->return404"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>返回http404。在Controller中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		$this->controller->return404()
	}
	
}
</pre>

<div class="nav"></div>
<div class="title">6. $this->controller->return200() 返回http200<a name="$this->controller->return200"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>返回http200。在Controller中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		$this->controller->return200()
	}
	
}
</pre>

<div class="nav"></div>
<div class="title">7. $this->controller->return500() 返回http500<a name="$this->controller->return500"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>返回http500。在Controller中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		$this->controller->return500()
	}
	
}
</pre>

<div class="nav"></div>
<div class="title">8. $this->controller->return403() 返回http403<a name="$this->controller->return403"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>返回http403。在Controller中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		$this->controller->return403()
	}
	
}
</pre>

<div class="nav"></div>
<div class="title">9. $this->controller->return405() 返回http405<a name="$this->controller->return405"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>返回http405。在Controller中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		$this->controller->return405()
	}
	
}
</pre>

<div class="nav"></div>
<div class="title">10. $this->controller->check_service_return() 验证Service返回的结构是否正确<a name="$this->controller->check_service_return"></a></div>
<div class="title">
  <h3>接口描述：</h3>
</div>
<p>验证Service返回的结构是否正确。在Controller中使用</p>
<div class="title">
  <h3>使用：</h3>
</div>
<pre id="PHP" class="prettyprint">
class indexController extends Controller {
	
	public $initphp_list = array('test'); 
	
	public function run() {
		if ($this->controller->check_service_return($ret)) {
			return true;
		} else {
			return false;
		}
	}
	
}
</pre>

<div class="nav"></div>
<div class="footer">版权所有：<a href="">http://initphp.com</a> InitPHP (A PHP Framework) By @Aliyun_zhuli</div>
<script src="static/jquery.js" type="text/javascript"></script>
<link href="static/prettify/prettify.css" rel="stylesheet" type="text/css">
<script src="static/prettify/prettify.js" type="text/javascript"></script>
<script type="text/javascript" src="static/comm.js"></script>
</body>
</html>
